package com.quanyan.runningriding.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.runningriding.entity.db.TbRunUser;
import com.quanyan.runningriding.entity.db.TbRunUserExample;
import com.quanyan.runningriding.entity.vo.run.resp.RunUserInfo;
import com.quanyan.runningriding.mapper.TbRunUserMapper;
import com.quanyan.runningriding.service.RunEditProfileService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by liubin on 2016/12/19.
 */
@Service
public class RunEditProfileServiceImpl implements RunEditProfileService {
    @Autowired
    TbRunUserMapper tbRunUserMapper;

    @Override
    public APIResponse<?> editProfile(String userId, String photo, String nickname, Byte gender, Double height, Double weight, Integer age) {
        if (StringUtil.isBlankOrNull(userId)){
            return APIResponse.returnFail("用户Id不能为空");
        }
        TbRunUser tbRunUser = new TbRunUser();
        tbRunUser.setUserId(userId);
        tbRunUser.setPhoto(photo);
        tbRunUser.setNickname(nickname);
        tbRunUser.setGender(gender);

        if (height != 0.0 && height >= 1.00 && height <= 2.30){
            tbRunUser.setHeight(height);
        }else {
            tbRunUser.setHeight(1.70);
        }

        if (weight != 0.0 && weight >= 25.0 && weight <=200){
            tbRunUser.setWeight(weight);
        }else {
            tbRunUser.setWeight(65.0);
        }

        if (age != 0 && age >= 1930 && age <= 2006){
            tbRunUser.setAge(age);
        }else {
            tbRunUser.setAge(1985);
        }

        tbRunUser.setUpdateTime(new Date());
        TbRunUserExample tbRunUserExample = new TbRunUserExample();
        tbRunUserExample.createCriteria().andUserIdEqualTo(userId);
        List<TbRunUser> tbRunUsers = tbRunUserMapper.selectByExample(tbRunUserExample);
        if (null != tbRunUsers && tbRunUsers.size() > 0){
            tbRunUserMapper.updateByExampleSelective(tbRunUser,tbRunUserExample);
            return APIResponse.returnSuccess("用户资料更新成功");
        }else {
            return APIResponse.returnFail("用户不存在");
        }
    }

    @Override
    public APIResponse<RunUserInfo> displayUserInfo(String userId) {
        TbRunUserExample tbRunUserExample = new TbRunUserExample();
        tbRunUserExample.createCriteria().andUserIdEqualTo(userId);
        List<TbRunUser> tbRunUsers = tbRunUserMapper.selectByExample(tbRunUserExample);
        TbRunUser tbRunUser = null;
        RunUserInfo runUserInfo = new RunUserInfo();
        if (null != tbRunUsers && tbRunUsers.size() > 0){
           tbRunUser =  tbRunUsers.get(0);
            runUserInfo.setPhoto(tbRunUser.getPhoto());
            runUserInfo.setNickname(tbRunUser.getNickname());
            runUserInfo.setGender(tbRunUser.getGender());
            runUserInfo.setHeight(tbRunUser.getHeight());
            runUserInfo.setWeight(tbRunUser.getWeight());
            runUserInfo.setAge(tbRunUser.getAge());
        }
        return APIResponse.returnSuccess(runUserInfo);
    }

    @Override
    public APIResponse bindingAccount(Byte accountType) {
        return null;
    }
}
